


void EPD_init() {

  //--------------EPD INIT-------------//
  digitalWrite(RST_PIN, HIGH);
  pinMode(RST_PIN, OUTPUT);
  delay(20);
  digitalWrite(RST_PIN, LOW);
  delay(20);
  digitalWrite(RST_PIN, HIGH);
  delay(200);
  display1.init(115200, false, 10, false); // default 10ms reset pulse, e.g. for bare panels with DESPI-C02
  display2.init(115200, false, 10, false);
  //void init(uint32_t serial_diag_bitrate, bool initial, bool pulldown_rst_mode = false)
  //You could use initial = true for normal processor reset, initial = false after deep sleep wakeup reset.
  //display.init(115200, false, 2, false); // USE THIS for Waveshare boards with "clever" reset circuit, 2ms reset pulse
  display1.setRotation(3);//设置屏幕旋转1和3是横向  0和2是纵向
  display2.setRotation(1);
  u8g2Fonts1.begin(display1);                        // 将u8g2过程连接到Adafruit GFX
  u8g2Fonts2.begin(display2);
  //u8g2Fonts.setFontMode(1);                        // 使用u8g2透明模式（这是默认设置）
  u8g2Fonts1.setFontDirection(0);
  u8g2Fonts2.setFontDirection(0);
  // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall

}

//Hello测试用

void helloWorld(GxEPD2_GFX& display, U8G2_FOR_ADAFRUIT_GFX& u8g2Fonts)
{
  //Serial.println("helloWorld");
  u8g2Fonts.setFont(SolarDate5_36);//
  u8g2Fonts.setForegroundColor(GxEPD_RED); //GxEPD_RED  // 设置前景色
  u8g2Fonts.setBackgroundColor(GxEPD_WHITE); //GxEPD_WHITE  // 设置背景色
  uint16_t x = (display.width() - 200) / 2;
  uint16_t y = display.height() / 2;
  display.setFullWindow();
  display.firstPage();
  do
  {
    u8g2Fonts.setCursor(x, y);
    u8g2Fonts.print("192 168 4 1");
  }
  while (display.nextPage());
  //Serial.println("helloWorld done");
}



unsigned int return_week_day(unsigned int year, unsigned int month, unsigned int day)
{
  int week = 0;
  if ( month == 1 || month == 2) {
    month += 12;
    year--;
  }
  //使用基姆拉尔森计算公式
  week = ( day + 2 * month + 3 * ( month + 1 ) / 5 + year + year / 4 - year / 100 + year / 400 ) % 7;

  return week + 1;
}

#include "lunar.h"

void dateDisplay(GxEPD2_GFX& display, U8G2_FOR_ADAFRUIT_GFX& u8g2Fonts) {

  String weekString = "", dateString = "";
  dateString += String(r_year);
  dateString += "年";
  dateString += String(r_month);
  dateString += "月";

  r_weekday = return_week_day(r_year, r_month, r_day);
  //weekString = "星期";
  if      (r_weekday == 1)weekString = "一";
  else if (r_weekday == 2)weekString = "二";
  else if (r_weekday == 3)weekString = "三";
  else if (r_weekday == 4)weekString = "四";
  else if (r_weekday == 5)weekString = "五";
  else if (r_weekday == 6)weekString = "六";
  else if (r_weekday == 7)weekString = "日";



  //----------计算农历----------->>>>>>>>>>>>>>>>>>>>>>-------------------//
  static struct Lunar_Date Lunar;
  LUNAR_SolarToLunar (&Lunar, r_year, r_month, r_day);

  //  Serial.print(r_year); Serial.print("年");
  //  Serial.print(r_month); Serial.print("月");
  //  Serial.print(days); Serial.print("日 星期");
  //  Serial.print(weekdays); Serial.print(" ");
  //  Serial.print(Lunar_MonthLeapString[Lunar.IsLeap]);
  //  Serial.print(Lunar_MonthString[Lunar.Month]);
  //  Serial.print(Lunar_DateString[Lunar.Date]);
  //  Serial.println("");
  //  Serial.print (Lunar_StemStrig[LUNAR_GetStem (&Lunar)]);
  //  Serial.print (Lunar_BranchStrig[LUNAR_GetBranch (&Lunar)]);
  //  Serial.print (Lunar_ZodiacString[LUNAR_GetZodiac (&Lunar)]);
  //  Serial.println ("年");

  String LunarLine1 = String(Lunar_StemStrig[LUNAR_GetStem (&Lunar)])
                      + String(Lunar_BranchStrig[LUNAR_GetBranch (&Lunar)])
                      + "[" + String(Lunar_ZodiacString[LUNAR_GetZodiac (&Lunar)]) + "]年"
                      + String(Lunar_MonthLeapString[Lunar.IsLeap])
                      + String(Lunar_MonthString[Lunar.Month])
                      + String(Lunar_DateString[Lunar.Date]);

  uint8_t dayleft = 0;
  uint8_t JQday = GetJieQiStr (r_year, r_month, r_day, &dayleft);
  String LunarLine2 = "";
  if (dayleft == 0)
  {
    //    Serial.print ("今日");
    //    Serial.println (JieQiStr[JQday]);
    LunarLine2 += "今日";
    LunarLine2 += "[";
    LunarLine2 += String(JieQiStr[JQday]);
    LunarLine2 += "]";
  }
  else
  {
    //        Serial.print ("距离");
    //        Serial.print(JieQiStr[JQday]);
    //        Serial.print(day);
    //        Serial.println ("天");
    //LunarLine2 += "距离";
    LunarLine2 += "[";
    LunarLine2 += String(JieQiStr[JQday]);
    LunarLine2 += "]还有";
    if      (dayleft == 1)LunarLine2 += "一";
    else if (dayleft == 2)LunarLine2 += "二";
    else if (dayleft == 3)LunarLine2 += "三";
    else if (dayleft == 4)LunarLine2 += "四";
    else if (dayleft == 5)LunarLine2 += "五";
    else if (dayleft == 6)LunarLine2 += "六";
    else if (dayleft == 7)LunarLine2 += "七";
    else if (dayleft == 8)LunarLine2 += "八";
    else if (dayleft == 9)LunarLine2 += "九";
    else if (dayleft == 10)LunarLine2 += "十";
    else if (dayleft == 11)LunarLine2 += "十一";
    else if (dayleft == 12)LunarLine2 += "十二";
    else if (dayleft == 13)LunarLine2 += "十三";
    else if (dayleft == 14)LunarLine2 += "十四";
    else if (dayleft == 15)LunarLine2 += "十五";
    LunarLine2 += "天";
  }

  //----------墨水屏显示部分----------->>>>>>>>>>>>>>>>>>>>>>-------------------//
  display.firstPage();
  do {
    u8g2Fonts.setFont(SolarDate5_36);//公历字体
    u8g2Fonts.setForegroundColor(GxEPD_BLACK); //GxEPD_RED  // 设置前景色
    u8g2Fonts.setBackgroundColor(GxEPD_WHITE); //GxEPD_WHITE  // 设置背景色

    //    display.fillScreen(GxEPD_RED);
    //    u8g2Fonts.setForegroundColor(GxEPD_WHITE); //GxEPD_RED  // 设置前景色
    //    u8g2Fonts.setBackgroundColor(GxEPD_RED); //GxEPD_WHITE  // 设置背景色
    uint16_t x3 = ((display.width() - u8g2Fonts.getUTF8Width(dateString.c_str())) / 2);
    u8g2Fonts.setCursor(x3, 70);
    u8g2Fonts.print(dateString);

    if (r_day < 10) {
      display.drawInvertedBitmap(62, 100, gImage_[0], 88, 112/* (88 X 112 , Arial Black )*/, GxEPD_RED);//画图x,y,字代码,字符宽，字符高
      display.drawInvertedBitmap(150, 100, gImage_[r_day], 88, 112, GxEPD_RED);//画图
    } else if (r_day >= 10 && r_day < 20) {
      display.drawInvertedBitmap(62, 100, gImage_[1], 88, 112, GxEPD_RED);//画图
      display.drawInvertedBitmap(150, 100, gImage_[r_day - 10], 88, 112, GxEPD_RED);//画图
    } else if (r_day >= 20 && r_day < 30) {
      display.drawInvertedBitmap(62, 100, gImage_[2], 88, 112, GxEPD_RED);//画图
      display.drawInvertedBitmap(150, 100, gImage_[r_day - 20], 88, 112, GxEPD_RED);//画图
    } else {
      display.drawInvertedBitmap(62, 100, gImage_[3], 88, 112, GxEPD_RED);//画图
      display.drawInvertedBitmap(150, 100, gImage_[r_day - 30], 88, 112, GxEPD_RED);//画图
    }

    u8g2Fonts.setForegroundColor(GxEPD_BLACK);  // 设置前景色
    u8g2Fonts.setBackgroundColor(GxEPD_WHITE);  // 设置背景色
    u8g2Fonts.setCursor(98, 270);//标记：位置需要修改
    u8g2Fonts.print("星期");
    if (r_weekday == 6 || r_weekday == 7) {
      u8g2Fonts.setForegroundColor(GxEPD_RED);  // 设置前景色
      u8g2Fonts.setBackgroundColor(GxEPD_WHITE);  // 设置背景色
    }
    u8g2Fonts.setCursor(168, 270);
    u8g2Fonts.print(weekString);

    u8g2Fonts.setFont(u8g2_font_LunarFont_28);//设置农历字体
    u8g2Fonts.setForegroundColor(GxEPD_BLACK);  // 设置前景色
    u8g2Fonts.setBackgroundColor(GxEPD_WHITE);  // 设置背景色

    //计算字符长度居中
    uint16_t x1 = ((display.width() - u8g2Fonts.getUTF8Width(LunarLine1.c_str())) / 2);
    u8g2Fonts.drawUTF8(x1, 320, LunarLine1.c_str());//另一种显示方式
    //  u8g2Fonts.setCursor(x1, 325);//标记：位置需要修改
    //  u8g2Fonts.print(LunarLine1);

    u8g2Fonts.setForegroundColor(GxEPD_RED);  // 设置前景色
    u8g2Fonts.setBackgroundColor(GxEPD_WHITE);  // 设置背景色
    uint16_t x2 = ((display.width() - u8g2Fonts.getUTF8Width(LunarLine2.c_str())) / 2);
    u8g2Fonts.setCursor(x2, 355);//标记：位置需要修改
    u8g2Fonts.print(LunarLine2);
  }
  while (display.nextPage());
  //---------------------<<<<<<<<<<<<<<<<<<<<<<-------------------//

}


void drawWeatherICON (GxEPD2_GFX& display, String weather) {
  uint8_t x = 86;
  uint8_t y = 20;
  //Serial.print("时间："); Serial.println(r_hour);
  if (weather == "晴") {
    if (r_hour >= 6 && r_hour < 18) {
      display.drawInvertedBitmap(x, y, sunny, 128, 128, GxEPD_RED);//画图
    } else {
      display.drawInvertedBitmap(x, y, clearnight, 128, 128, GxEPD_RED);//画图
    }
  } else if ( weather == "少云" || weather == "晴间多云") {
    if (r_hour >= 6 && r_hour < 18) {
      display.drawInvertedBitmap(x, y, partlycloudy_R, 128, 128, GxEPD_RED);//画图
      display.drawInvertedBitmap(x, y, partlycloudy_B, 128, 128, GxEPD_BLACK);//画图
    } else {
      display.drawInvertedBitmap(x, y, partlycloudy_night_R, 128, 128, GxEPD_RED);//画图
      display.drawInvertedBitmap(x, y, partlycloudy_B, 128, 128, GxEPD_BLACK);//画图
    }
  } else if (weather == "多云" || weather == "阴") {
    display.drawInvertedBitmap(x, y, cloudy, 128, 128, GxEPD_BLACK);//画图
  } else if (weather == "有风" || weather == "平静" || weather == "微风" || weather == "和风" || weather == "清风" || weather == "强风/劲风" || weather == "疾风" || weather == "大风" || weather == "烈风" || weather == "风暴" || weather == "狂爆风" || weather == "飓风" || weather == "热带风暴" || weather == "浮尘" || weather == "扬沙" || weather == "沙尘暴" || weather == "强沙尘暴" || weather == "龙卷风") {
    display.drawInvertedBitmap(x, y, windy, 128, 128, GxEPD_BLACK);//画图
  } else if (weather == "霾" || weather == "中度霾" || weather == "重度霾" || weather == "严重霾" || weather == "雾" || weather == "浓雾" || weather == "强浓雾" || weather == "轻雾" || weather == "大雾" || weather == "特强浓雾") {
    display.drawInvertedBitmap(x, y, fog, 128, 128, GxEPD_BLACK);//画图
  } else if (weather == "阵雨" || weather == "雷阵雨" || weather == "雷阵雨并伴有冰雹" || weather == "强阵雨" || weather == "强雷阵雨" || weather == "极端降雨") {
    display.drawInvertedBitmap(x, y, thunderstorm_R, 128, 128, GxEPD_RED);//画图
    display.drawInvertedBitmap(x, y, thunderstorm_B, 128, 128, GxEPD_BLACK);//画图
  } else if (weather == "小雨" || weather == "中雨" || weather == "毛毛雨/细雨" || weather == "雨" || weather == "小雨-中雨" || weather == "雨雪天气" || weather == "雨夹雪" || weather == "阵雨夹雪" || weather == "冻雨") {
    display.drawInvertedBitmap(x, y, lightrain, 128, 128, GxEPD_BLACK);//画图
  } else if (weather == "大雨" || weather == "暴雨" || weather == "大暴雨" || weather == "特大暴雨" || weather == "中雨-大雨" || weather == "大雨-暴雨" || weather == "暴雨-大暴雨" || weather == "大暴雨-特大暴雨") {
    display.drawInvertedBitmap(x, y, rainy, 128, 128, GxEPD_BLACK);//画图
  } else if (weather == "雪" || weather == "阵雪" || weather == "小雪" || weather == "中雪" || weather == "大雪" || weather == "暴雪" || weather == "小雪-中雪" || weather == "中雪-大雪" || weather == "大雪-暴雪") {
    display.drawInvertedBitmap(x, y, snowy, 128, 128, GxEPD_BLACK);//画图
  } else if (weather == "热" || weather == "冷" || weather == "未知") {
    display.drawInvertedBitmap(x, y, attention_R, 128, 128, GxEPD_RED);//画图
    display.drawInvertedBitmap(x, y, attention_B, 128, 128, GxEPD_BLACK);//画图
  }

}


void WeatherDisplay(GxEPD2_GFX& display, U8G2_FOR_ADAFRUIT_GFX& u8g2Fonts) {

  String windString = WeatherLive.winddirection + "风" + WeatherLive.windpower + "级" + " 湿度" + WeatherLive.humidity;
  String tempRange = Forcasts_0.daytemp + "°C/" + Forcasts_0.nighttemp + "°C";
  String weatherRange = "白天" + Forcasts_0.dayweather + " / 夜间" + Forcasts_0.nightweather;

  display.firstPage();
  do
  {
    drawWeatherICON (display, WeatherLive.weather);

    if (WeatherLive.temperature <= -10) {
      display.drawBitmap(58, 150, tImage_negtive, 32, 96, GxEPD_BLACK);
      display.drawBitmap(90, 150, tImage_[- WeatherLive.temperature / 10 % 10], 40, 96, GxEPD_BLACK);
      display.drawBitmap(130, 150, tImage_[- WeatherLive.temperature % 10], 40, 96, GxEPD_BLACK);
    }
    if ((WeatherLive.temperature > -10) && (WeatherLive.temperature < 0)) {
      display.drawBitmap(98, 150, tImage_negtive, 32, 96, GxEPD_BLACK);
      display.drawBitmap(130, 150, tImage_[- WeatherLive.temperature % 10], 40, 96, GxEPD_BLACK);
    }
    if ((WeatherLive.temperature >= 0) && (WeatherLive.temperature < 10)) {
      display.drawBitmap(130, 150, tImage_[WeatherLive.temperature % 10], 40, 96, GxEPD_BLACK);
    }
    if ((WeatherLive.temperature >= 10) && (WeatherLive.temperature < 30)) {
      display.drawBitmap(90, 150, tImage_[WeatherLive.temperature / 10 % 10], 40, 96, GxEPD_BLACK);
      display.drawBitmap(130, 150, tImage_[WeatherLive.temperature % 10], 40, 96, GxEPD_BLACK);
    }
    if (WeatherLive.temperature >= 30) {
      display.drawBitmap(90, 150, tImage_[WeatherLive.temperature / 10 % 10], 40, 96, GxEPD_RED);
      display.drawBitmap(130, 150, tImage_[WeatherLive.temperature % 10], 40, 96, GxEPD_RED);
    }
    if (WeatherLive.temperature < 30) {
      display.drawBitmap(170, 140, tImage_du, 80, 96, GxEPD_BLACK);
    } else {
      display.drawBitmap(170, 140, tImage_du, 80, 96, GxEPD_RED);
    }
    u8g2Fonts.setForegroundColor(GxEPD_BLACK);  // 设置前景色
    u8g2Fonts.setBackgroundColor(GxEPD_WHITE);  // 设置背景色
    u8g2Fonts.setFont(weather_30);
    u8g2Fonts.setCursor(170, 215);//标记：位置需要修改
    u8g2Fonts.print(WeatherLive.weather);

    u8g2Fonts.setFont(u8g2_font_tempRange_36);
    u8g2Fonts.setForegroundColor(GxEPD_RED);  // 设置前景色
    uint16_t x1 = ((display.width() - u8g2Fonts.getUTF8Width(tempRange.c_str())) / 2);
    u8g2Fonts.setCursor(x1, 275);//标记：位置需要修改
    u8g2Fonts.print(tempRange);

    u8g2Fonts.setFont(u8g2_font_weather_26);
    u8g2Fonts.setForegroundColor(GxEPD_BLACK);  // 设置前景色
    u8g2Fonts.setBackgroundColor(GxEPD_WHITE);  // 设置背景色
    uint16_t x3 = ((display.width() - u8g2Fonts.getUTF8Width(weatherRange.c_str())) / 2);
    u8g2Fonts.setCursor(x3, 315);//标记：位置需要修改
    u8g2Fonts.print(weatherRange);

    //u8g2Fonts.setFont(u8g2_font_wind_28);
    uint16_t x2 = ((display.width() - u8g2Fonts.getUTF8Width(windString.c_str())) / 2);
    u8g2Fonts.setCursor(x2, 350);//标记：位置需要修改
    u8g2Fonts.print(windString);
    
//    u8g2Fonts.setCursor(x2, 385);//标记：位置需要修改
//    u8g2Fonts.print(r_hour);

  }
  while (display.nextPage());
}
